//
//  DNUserContext.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/2/5.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import UIKit

class DNUserContext: NSObject {
    
    /// 登录Token
    var token : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "SessionKey")
            UserDefaults.standard.synchronize()
        }
        get{
            if UserDefaults.standard.value(forKey: "SessionKey") == nil {
                return ""
            }else{
                return UserDefaults.standard.value(forKey: "SessionKey") as! String
            }
        }
    }
    
    /// 用户是否登录
    var isLogin : Bool?{
        get{
            return self.token!.lengthOfBytes(using: .utf8) > 0
        }
    }
    
    /// 用户电话号码
    var userMobile : NSString?{
        set{
            UserDefaults.standard.setValue(newValue, forKey: "userMobile")
            UserDefaults.standard.synchronize()
        }
        get{
            return UserDefaults.standard.value(forKey: "userMobile") as? NSString ?? ""
        }
        
    }
    
    /// 用户ID
    var ID : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "ID")
            UserDefaults.standard.synchronize()
        }
        get{
            return UserDefaults.standard.value(forKey: "ID") as? String
        }
    }
    var idCard : String?
    
    /// 用户头像
    var Avatar : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "Avatar")
            UserDefaults.standard.synchronize()
        }
        get{
            return UserDefaults.standard.value(forKey: "Avatar") as? String
        }
    }
    
    /// 用户ID
    var UserId : String?
    
    /// 用户会员ID
    var MemberId : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "MemberId")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "MemberId") as! String)
        }
    }
    var TenantID : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "TenantID")
            UserDefaults.standard.synchronize()
        }
        get{
            return UserDefaults.standard.value(forKey: "TenantID") as? String
            
        }
    }
    var RealName : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "RealName")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "RealName") as! String)
        }
    }
    var NickName : String?{
        set{
            UserDefaults.standard.set(newValue ?? "", forKey: "NickName")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "NickName") as! String)
        }
    }
    
    var openid : String?
    var sex : String?
    var unionid : String?
    var province : String?
    var country : String?
    var city : String?
    var ChannelName : String? {
        set{
            UserDefaults.standard.set(newValue ?? "", forKey: "ChannelName")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "ChannelName") as! String)
        }
    }
    var Point : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "Point")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "Point") as! String)
        }
    }
    var wechatIcon : String?
    var wechatNickName : String?{
        set{
            UserDefaults.standard.setValue(newValue ?? "", forKey: "wechatNickName")
            UserDefaults.standard.synchronize()
        }
        get{
            return (UserDefaults.standard.value(forKey: "wechatNickName") as! String)
        }
    }
    var weixinCode : String?
    var isWebWXAuth : Bool?
    
    var userQRCodeURLStr : String?
    var saleInputSupplyCompanyName : String?
    var saleInputSupplyCompanyID : String?
    var returnInputSupplyCompanyName : String?
    var returnInputSupplyCompanyID : String?
    
    
    func cleanUserInfo(){
        
        UserDefaults.standard.removeObject(forKey: "NickName")
        UserDefaults.standard.removeObject(forKey: "SessionKey")
        
        UserDefaults.standard.removeObject(forKey: "RealName")
        UserDefaults.standard.removeObject(forKey: "TenantID")
        
        UserDefaults.standard.removeObject(forKey: "MemberId")
        UserDefaults.standard.removeObject(forKey: "userMobile")
        
        UserDefaults.standard.removeObject(forKey: "ID")
        UserDefaults.standard.removeObject(forKey: "Avatar")
        
        UserDefaults.standard.synchronize()
    }
    
    func kIsLogin() -> Bool{
        return self.token!.lengthOfBytes(using: .utf8) > 0
    }
    

    static let sharedInstance = DNUserContext()
    private override init() {}
    
}
